#include "StdAfx.h"
#include "Game.h"

#include "sys/device.h"
#include "sys/TouchPad.h"
#include "sys/KeyPad.h"
#include "sys/Graphics.h"
#include "sys/System.h"

#include "sys/File.h"
#include "sys/HWFont.h"
#include "sys/Timer.h"

#include "engine/SimpleObj.h"
#include "engine/Mesh.h"
#include "sound/ModPlayer.h"

CGame CGame::__this;

CGame::CGame()
{
	m_bufferMain = NULL;
	m_bufferSub = NULL;
	
	m_graphicsMain = NULL;
	m_graphicsSub = NULL;
}	

CGame::~CGame()
{

}

CMesh mesh;

BOOL CGame::init()
{
	CTouchPad::init();
	CTouchPad::start();
	CTimer::init();

	m_bufferMain = new u16[ SCR_W * SCR_H ];
	m_bufferSub	= new u16[ SCR_W * SCR_H ];

	m_graphicsMain = new CGraphics(m_bufferMain, SCR_W, SCR_H);
	m_graphicsSub = new CGraphics(m_bufferSub, SCR_W, SCR_H);		
	
	CFile *f = CFile::open("mod.lib");
	CModPlayer::getInstance()->loadFile(f);
	CFile::close(f);
	
	f = CFile::open("human.lib");
	mesh.load(f);
	CFile::close(f);
	
	return TRUE;
}
	
u8 v;
u16 Rotate = 0;
void CGame::timerCallback()
{
	CTimer::update();	//should be first
	CTouchPad::update();
	CKeyPad::update();

	CGraphics *g = getGraphicsUp();
	
//	g->setColor(v, 0, 0);
//	g->fillRect(0,0,SCR_W,SCR_H);
	
	g = getGraphicsDown();
	
	g->setColor(0, 0, 255 - v);
//	g->fillRect(0,0,SCR_W,SCR_H);
	g->fillRect(10,10,24,8);
	
	v+=8;
	
	if (CTouchPad::isTouched())
	{
		g->setColor(255,255,255);
		g->fillRect(CTouchPad::getX()-3, CTouchPad::getY()-3,6,6);
	}
	

	g->setColor(255,255,0);
	
	if (CKeyPad::isKeyDown(KEY_L))
		g->fillRect(0,0,30,20);	
	if (CKeyPad::isKeyDown(KEY_R))
		g->fillRect(SCR_W-30,0,30,20);
	
	if (CKeyPad::isKeyDown(KEY_UP))
		g->fillRect(40,50,10,30);
	if (CKeyPad::isKeyDown(KEY_DOWN))
		g->fillRect(40,90,10,30);
	if (CKeyPad::isKeyDown(KEY_LEFT))
		g->fillRect(10,80,30,10);
	if (CKeyPad::isKeyDown(KEY_RIGHT))
		g->fillRect(50,80,30,10);
	
	
	if (CKeyPad::isKeyDown(KEY_X))
		g->fillRect(190,50,10,10);
	if (CKeyPad::isKeyDown(KEY_B))
		g->fillRect(190,110,10,10);
	if (CKeyPad::isKeyDown(KEY_Y))
		g->fillRect(160,80,10,10);
	if (CKeyPad::isKeyDown(KEY_A))
		g->fillRect(220,80,10,10);
	

	if (CKeyPad::isKeyDown(KEY_START))
		g->fillRect(180,150,30,10);
	if (CKeyPad::isKeyDown(KEY_SELECT))
		g->fillRect(180,170,30,10);
	
	
	char str[12];
	sprintf(str,"FPS: %03d", CTimer::getFPSx10());
	g->setColor(0,255,0);
	CHWFont::drawString(g,str,10,10);
		
		
    G3X_Reset();
    Rotate += 256;

    CSimpleObj::simpleCamera();

	CSimpleObj::simpleLight();

    G3_PushMtx();

    fx16    s = FX_SinIdx(Rotate);
    fx16    c = FX_CosIdx(Rotate);

    G3_Translate(0, 0, -5 * FX32_ONE);

    G3_RotX(s, c);
    G3_RotY(s, c);
    G3_RotZ(s, c);

	CSimpleObj::simpleMaterial();
	CSimpleObj::textureScale(8,8);
	CSimpleObj::useCheckerTex();
	CSimpleObj::renderCube();
	
	G3_PushMtx();
	G3_Scale(FX32_ONE/3, FX32_ONE/3, FX32_ONE/3);
	mesh.render();
	G3_PopMtx(1);
    G3_PopMtx(1);
    
    CSystem::getInstance()->flip3DScreen();		
}

void CGame::frameDelay()
{
//	CTimer::limit(40);
}

